package com.huangwenshuo.futurestarschoolmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huangwenshuo.futurestarschoolmanagement.exception.CustomRuntimeException;
import com.huangwenshuo.futurestarschoolmanagement.pojo.dto.WorkDto;
import com.huangwenshuo.futurestarschoolmanagement.pojo.po.WorksPo;
import com.huangwenshuo.futurestarschoolmanagement.mapper.WorksMapper;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.ActivitysVo;
import com.huangwenshuo.futurestarschoolmanagement.pojo.vo.WorksVo;
import com.huangwenshuo.futurestarschoolmanagement.service.WorksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangwenshuo
 * @since 2021-03-06
 */
@Service
public class WorksServiceImpl extends ServiceImpl<WorksMapper, WorksPo> implements WorksService {

    @Autowired
    private WorksMapper worksMapper;
    @Override
    public void addWork(WorkDto.AddWork addWork) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("aid",addWork.getAid());
        queryWrapper.eq("uid",addWork.getUid());
        if(worksMapper.selectOne(queryWrapper) != null){
            throw new CustomRuntimeException("该作品已提交");
        }
        worksMapper.addWork(addWork);
    }

    @Override
    public void upaWork(WorkDto.AddWork addWork) {
        worksMapper.upaWork(addWork);
    }

    @Override
    public Map<String, Object> getMyWorks(WorkDto.GetWork getWork) {
        Map<String, Object> res = new HashMap<>();
        int page = getWork.getPage();
        getWork.setPage((getWork.getPage()-1)*getWork.getRows());
        getWork.setRows(page*getWork.getRows());
        List<WorksVo> worksVos = worksMapper.getMyWorks(getWork);
        Integer total = worksMapper.getMyWorksTotal(getWork);
        res.put("works",worksVos);
        res.put("total",total);
        return res;
    }

    @Override
    public  List<WorksVo> getShowMyWorks(Integer uid) {

        return worksMapper.getShowMyWorks(uid);
    }

    @Override
    public void updateShow(Integer uid, Integer aid, Integer show) {
        worksMapper.updateShow(uid,aid,show);
    }
}
